package cn.dansj.handler.interfaces;

import cn.dansj.annotation.MarkAsApi;
import cn.dansj.common.utils.json.DictMap;
import cn.dansj.handler.InterfaceHandler;
import cn.dansj.common.utils.transfer.Verification;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@MarkAsApi
public class EdgeBookmarks extends InterfaceHandler {
    private static final String select = "select * from x_edge_component where component in ('count-down','site')";

    @Override
    public Object execute(Map<String, Object> paramsMap) {
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(select);
        maps.forEach(map -> map.put("prop", DictMap.parseObject(map.remove("prop"), new DictMap.JavaType<Map<?, ?>>() {
        })));

        List<Map<String, Object>> parent = maps.stream().filter(e -> Verification.checkNull(e.get("parent_id"))).toList();

        List<Map<String, Object>> child = maps.stream().filter(e -> Verification.checkNotNull(e.get("parent_id"))).toList();

        for (Map<String, Object> parentMap : parent) {
            List<Map<String, Object>> children = new ArrayList<>();

            for (Map<String, Object> childMap : child) {
                if (childMap.get("parent_id").equals(parentMap.get("id"))) {
                    children.add(childMap);
                }
            }
            parentMap.put("children", children);
        }
        return parent;
    }
}